Code
# Load package(s)
library(tidyverse)
library(tigris)
library(sf)
library(viridis)
library(statebins)
library(tidycensus)
# Load dataset(s)
load('data/US_income.rda')Data Visualization (STAT 302)
The goal of this lab is to explore various ways of building maps with ggplot2.
Challenges are not mandatory for students to complete. We highly recommend students attempt them though. We would expect graduate students to attempt the challenges.
We’ll be using the US_income.rda dataset which should be placed in the /data subdirectory in our data_vis_labs project. You’ll also be downloading your own data to build maps.
# Load package(s)
library(tidyverse)
library(tigris)
library(sf)
library(viridis)
library(statebins)
library(tidycensus)
# Load dataset(s)
load('data/US_income.rda')Make a county map of a US state using geom_polygon(). Maybe use your home state or a favorite state. Please do NOT use the state in the ggplot2 book example.
Optional: Consider adding major cities (or your home town).
Hints:
Now use geom_sf() instead. You’ll need to download data for this. You can use either the tigris (github page) or geodata packages. Either tigris’ counties() with cb = TRUE or geodata’s gadm() could be useful.
Using the US_income dataset, recreate the following graphics as precisely as possible.
#eval : FALSE
# Setting income levels
US_income <- mutate(
US_income,
income_bins = cut(
ifelse(is.na(median_income), 25000, median_income),
breaks = c(0, 40000, 50000, 60000, 70000, 80000),
labels = c("< $40k", "$40k to $50k",
"$50k to $60k", "$60k to $70k", "> $70k"),
right = FALSE
)
)Hints:
geom_sf() — boundary color is "grey80" and size is 0.2viridis package (discrete = TRUE in scale_* function)Hints:
statebins::geom_statebins()viridis package (discrete = TRUE in scale_* function)Pick any city or foreign country to build a map for. You can dress it up or make it as basic as you want. Also welcome to try building a graphic like that depicted at the end of section 6.5 — use a different region though.
Optional
Using the tidycensus package and few others, try to create a map like below using these directions. Try using a different geographical area and a different variable from the ACS.